package com.example.myandroidapp1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Email extends Activity implements OnClickListener {

	private EditText etReceiverName;
	private EditText etReceiverEmail;
	private EditText etSenderName;
	private EditText etIntroText;
	private EditText etBody;

	private Button sendButton;

	private String receiverName;
	private String receiverEmail;
	private String senderName;
	private String introText;
	private String body;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.email);
		initialize();
		sendButton.setOnClickListener(this);
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bSend:
			populateFormData();
			String message = generateEmailMessage();
			Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
			emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{receiverEmail});
			emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test Email");
			emailIntent.setType("text/html");
			emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(message));
			startActivity(emailIntent);
			finish();
			break;
		}

	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	private void initialize() {
		etReceiverName = (EditText) findViewById(R.id.etRecName);
		etReceiverEmail = (EditText) findViewById(R.id.etRecEmail);
		etSenderName = (EditText) findViewById(R.id.etSendName);
		etIntroText = (EditText) findViewById(R.id.etIntro);
		etBody = (EditText) findViewById(R.id.etBody);
		sendButton = (Button) findViewById(R.id.bSend);
	}

	private String generateEmailMessage() {
		StringBuilder sb = new StringBuilder();
		sb.append("Hi ").append(receiverName).append("<br/>");
		sb.append("  <b>").append(introText).append("</b><br/>");
		sb.append(body).append("<br/><br/>");
		sb.append("Thanks,").append("<br/>");
		sb.append(senderName).append("<br/>");
		return sb.toString();
	}

	private void populateFormData() {
		receiverName = etReceiverName.getText().toString();
		receiverEmail = etReceiverEmail.getText().toString();
		senderName = etSenderName.getText().toString();
		introText = etIntroText.getText().toString();
		body = etBody.getText().toString();
	}
}
